package mts.testframework.tests;

import java.io.FileWriter;
import java.io.IOException;

import mts.ElevatorControllerInternalConnectionServer;
import mts.ElevatorControllerManager;
import mts.constants.Direction;
import mts.groupdispatcher.FloorRequest;
import mts.groupdispatcher.GroupDispatcher;
import mts.testframework.ScheduleCompletedListener;
import mts.testframework.TestItemScheduler;
import mts.testframework.testitems.FloorRequestTestItem;
import mts.testframework.testitems.HallCallTestItem;

public class FireEvacuationStressTest implements ScheduleCompletedListener  {
	private TestItemScheduler scheduler;
	private int numOfCars;
	
	public void startTest(GroupDispatcher groupDispatcher, ElevatorControllerInternalConnectionServer internalConnectionServer, int numOfCars) {
		this.numOfCars = numOfCars;
		ElevatorControllerManager.stopAllElevatorControllerProcesses();
		
		for (byte i = 0; i < numOfCars; i++) {
			ElevatorControllerManager.startElevatorProcess((byte)i);
		}
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		scheduler = new TestItemScheduler(groupDispatcher, internalConnectionServer);
		
		scheduler.schedule(
				new FloorRequestTestItem(0 , new FloorRequest((byte)9, Direction.DOWN), 
						new HallCallTestItem(0, (byte)0, (byte)1)),
				new FloorRequestTestItem(0 , new FloorRequest((byte)8, Direction.DOWN), 
						new HallCallTestItem(0, (byte)0, (byte)1)),
				new FloorRequestTestItem(0 , new FloorRequest((byte)7, Direction.DOWN), 
						new HallCallTestItem(0, (byte)0, (byte)1)),
				new FloorRequestTestItem(0 , new FloorRequest((byte)6, Direction.DOWN), 
						new HallCallTestItem(0, (byte)0, (byte)1)),
				new FloorRequestTestItem(0 , new FloorRequest((byte)5, Direction.DOWN), 
						new HallCallTestItem(0, (byte)0, (byte)1)),
				new FloorRequestTestItem(0 , new FloorRequest((byte)4, Direction.DOWN), 
						new HallCallTestItem(0, (byte)0, (byte)1)),
				new FloorRequestTestItem(0 , new FloorRequest((byte)3, Direction.DOWN), 
						new HallCallTestItem(0, (byte)0, (byte)1)),
				new FloorRequestTestItem(0 , new FloorRequest((byte)2, Direction.DOWN), 
						new HallCallTestItem(0, (byte)0, (byte)1))
				
				);
		scheduler.addOnCompletedListener(this);
		
	
	}
	
	@Override
	public void scheduleCompleted() {
		try {
			FileWriter fstream = new FileWriter("./stress tests/FireEvacuationStressTest" + numOfCars + ".txt");
			fstream.write(scheduler.getOutput());
			fstream.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
